﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Documents;

namespace Sora.HelpSystem.UI.WPF
{
    public class HelpText : BindingDecoratorBase
    {
        [DefaultValue(null)]
        public HelpDocumentSource DocumentSource { get; set; }

        [DefaultValue(null)]
        public string PageUri { get; set; }

        public override object ProvideValue(IServiceProvider provider)
        {
            if (DocumentSource == null)
                return PageUri;
            else
            {
                try
                {
                    if (DocumentSource.Document == null)
                        return PageUri;
                    else
                    {
                        var page = DocumentSource.DocumentProvider.ResolveUri(new Uri(PageUri));

                        if (page == null)
                            return PageUri;
                        else
                        {
                            var flowDoc = page.FlowContent.Source;
                            TextRange range = new TextRange(flowDoc.ContentStart, flowDoc.ContentEnd);
                            return range.Text;
                        }
                    }
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
        }
    }
}
